package com.lyshop.guiyingshanding.view;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.lyshop.guiyingshanding.R;
import com.lyshop.guiyingshanding.bean.OrderInfoBean;
import com.zc.mychart.utils.FormatUtil;

public class OrderSellDialog extends MyDialog implements View.OnClickListener {

    private View rootView;
    private View view_top;
    private TextView tv_cancel;
    private TextView tv_submit;
    private TextView tv_profit_loss;

    private OrderInfoBean mOrderInfoBean;
    private Context mContext;

    public OrderSellDialog(Context context, OrderInfoBean bean) {
        this(context, false, Gravity.BOTTOM, bean);
    }

    public OrderSellDialog(Context context, boolean isCancelAble, int gravity, OrderInfoBean bean) {
        super(context, isCancelAble, gravity);
        this.mContext = context;
        this.mOrderInfoBean = bean;
        Window w = this.getWindow();
        if (w != null) {
            w.setWindowAnimations(R.style.dialogTranslateAnim);
        }
        rootView = LayoutInflater.from(context).inflate(R.layout.dialog_order_sell, null);
        setContentView(rootView);

        view_top = rootView.findViewById(R.id.view_top);
        tv_cancel = rootView.findViewById(R.id.tv_cancel);
        tv_submit = rootView.findViewById(R.id.tv_submit);

        TextView tv_order_name = rootView.findViewById(R.id.tv_order_name);
        tv_profit_loss = rootView.findViewById(R.id.tv_profit_loss);

        String title = mOrderInfoBean.pro_name + " "
                + FormatUtil.formatDouble2(mOrderInfoBean.unit_price) + "元/件 " + mOrderInfoBean.amount + "件";
        tv_order_name.setText(title);

        setProfitLoss(mOrderInfoBean);
        tv_cancel.setOnClickListener(this);
        tv_submit.setOnClickListener(this);
        view_top.setOnClickListener(this);
    }

    public void setProfitLoss(OrderInfoBean mOrderInfoBean) {
        int redColor = ContextCompat.getColor(mContext, R.color.buy_red);
        int greenColor = ContextCompat.getColor(mContext, R.color.buy_green);
        if (mOrderInfoBean.profitAndLoss >= 0) {
            tv_profit_loss.setTextColor(redColor);
        } else {
            tv_profit_loss.setTextColor(greenColor);
        }
        tv_profit_loss.setText( FormatUtil.formatMoney(mOrderInfoBean.profitAndLoss));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.view_top:
                if (isCancelAble) {
                    dismiss();
                    mListener.onCancel();
                }
                break;
            case R.id.tv_cancel:
                dismiss();
                mListener.onCancel();
                break;
            case R.id.tv_submit:
                mListener.onConfirm();
                break;
        }
    }

    private ClickListener mListener;

    public interface ClickListener {

        void onConfirm();

        void onCancel();
    }

    public void setClickListener(ClickListener listener) {
        this.mListener = listener;
    }
}

